/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author puspa
 */
public class PanelFotoAnggota extends JPanel {

    private File file = null;
    private BufferedImage image = null;
    private JLabel labelKodeAnggota;
    private JTextField textKodeAnggota;
    private JButton tombolCariGambar;
    private JButton tombolSimpan;
    private JPanel panelTombol;

    public PanelFotoAnggota() {
        inisialisasi();
        try {
            this.file = new File("noPicture.png");
            image = ImageIO.read(file);
        } catch (IOException ex) {
            Logger.getLogger(PanelFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void inisialisasi() {
        setLayout(new BorderLayout());
        panelTombol = new JPanel();
        panelTombol.setLayout(new FlowLayout());
        labelKodeAnggota = new JLabel("Kode");
        panelTombol.add(labelKodeAnggota);
        textKodeAnggota = new JTextField();
        textKodeAnggota.setColumns(10);
        panelTombol.add(textKodeAnggota);
        tombolCariGambar = new JButton("CariGambar");
        panelTombol.add(tombolCariGambar);
        tombolSimpan = new JButton("Simpan");
        panelTombol.add(tombolSimpan);
        add(panelTombol, BorderLayout.SOUTH);

        tombolCariGambar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tombolCariGambarAction();
            }
        });
        tombolSimpan.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tombolSimpanAction();
            }
        });
        textKodeAnggota.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textKodeAnggotaAction();
            }
        });
    }

    public void textKodeAnggotaAction() {
        file = new File(textKodeAnggota.getText()+".PNG");
        repaint();
    }

    public void tombolSimpanAction() {
        File fileBaru = new File(textKodeAnggota.getText()+".png");
        try {
            FileOutputStream os = new FileOutputStream(fileBaru);
            FileInputStream is = new FileInputStream(file);
            // baca isi file lama, tulis ke file baru
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PanelFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    public void tombolCariGambarAction() {
        JFileChooser chooser = new JFileChooser();
        int status = chooser.showOpenDialog(this);
        if (status == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
            repaint();
        } else { //== JFileChooser.CANCEL_OPTION
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        try {
            image = ImageIO.read(file);
        } catch (IOException ex) {
            Logger.getLogger(PanelFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
        }
        g.drawImage(getImage(),
                50, 50, 300, 200,
                0, 0, getImage().getWidth(null), getImage().getHeight(null),
                null);

    }

    /**
     * @return the file
     */
    public File getFile() {
        return file;
    }

    /**
     * @param file the file to set
     */
    public void setFile(File file) {
        this.file = file;
    }

    /**
     * @return the image
     */
    public BufferedImage getImage() {
        return image;
    }

    /**
     * @param image the image to set
     */
    public void setImage(BufferedImage image) {
        this.image = image;
    }
}
